@extends('admin.base')
@section('content')
    <style>
        .layui-nav .layui-nav-item a {
            padding: 0 10px;
        }
        #log-nav {
            background-color: #eee!important;
        }
        #log-nav .layui-nav-item a{
            color: #333!important;
        }
        #log-nav .layui-nav-itemed>a, #log-nav .layui-nav-tree .layui-nav-title a {
            color: #333!important;
            /*background-color: #efefef!important;*/
        }
        #log-nav .layui-nav-tree a:hover, #log-nav .layui-nav-tree a:hover {
            color: #fff!important;
            /*background-color: #333;*/
        }

    </style>
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md2">
            <ul class="layui-nav layui-nav-tree layui-inline" lay-filter="log-nav" id="log-nav">
                @foreach($folders as $folder)
                    <li class="layui-nav-item layui-nav-itemed">
                        <a href="javascript:;" class="layui-nav-title layui-icon layui-icon-file-b" data-folder="{{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}"> {{$folder}}</a>
                        @if ($current_folder == $folder)
                            <dl class="layui-nav-child">
                                @foreach($folder_files as $file)
                                    <dd class="@if ($current_file == $file) layui-this @endif" style="padding-left: 20px">
                                        <a href="javascript:;" class="layui-nav-title layui-icon layui-icon-file" data-file="{{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}" data-folder="{{ \Illuminate\Support\Facades\Crypt::encrypt($folder) }}"> {{$file}}</a>
                                    </dd>
                                @endforeach
                            </dl>
                        @endif
                    </li>
                @endforeach
                @foreach($files as $file)
                    <li class="layui-nav-item @if($current_file == $file) layui-this @endif">
                        <a href="javascript:void(0)" class="layui-nav-title layui-icon layui-icon-file" data-file="{{ \Illuminate\Support\Facades\Crypt::encrypt($file) }}"> {{$file}}</a>
                    </li>
                @endforeach
            </ul>
        </div>
        <div class="layui-col-md10">
            <div class="layui-card">
                {{--<div class="layui-form layui-card-header layuiadmin-card-header-auto">--}}
                {{--<div class="layui-form-item pull-right">--}}
                {{--<div class="layui-inline">--}}
                {{--<div class="layui-input-inline">--}}
                {{--<input type="text" name="keyword" placeholder="请输入关键字查询" lay-verify="file" class="layui-input"/>--}}
                {{--</div>--}}
                {{--</div>--}}
                {{--<input type="hidden" name="l" class="layui-input" value="{{ $current_file ? \Illuminate\Support\Facades\Crypt::encrypt($current_file) : ''}}"/>--}}
                {{--<input type="hidden" name="f" class="layui-input" value="{{ $current_folder ? \Illuminate\Support\Facades\Crypt::encrypt($current_folder) : ''}}"/>--}}
                {{--<div class="layui-inline">--}}
                {{--<button class="layui-btn" id="searchBtn">搜 索</button>--}}
                {{--</div>--}}
                {{--</div>--}}
                {{--</div>--}}
                <div class="layui-card-body">
                    <table id="LAY-log-data" lay-filter="LAY-log-data"></table>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    @can('system.log')
        {{-- 设置 错误等级样式 --}}
        <script type="text/html" id="level">
            @{{#  if(d.level_class == 'danger'){ }}
            <span style="color:#FF5722" class="fa fa-@{{ d.level_img }}"
                  aria-hidden="true">  @{{ d.level }}</span>
            @{{#  } else if(d.level_class == 'warning') { }}
            <span style="color:#FFB800" class="fa fa-@{{ d.level_img }}"
                  aria-hidden="true">  @{{ d.level }}</span>
            @{{#  } else { }}
            <span style="color:#5FB878" class="fa fa-@{{ d.level_img }}"
                  aria-hidden="true">  @{{ d.level }}</span>
            @{{#  } }}
        </script>

        {{-- 工具栏按钮 --}}
        <script type="text/html" id="buttons">
            @can('system.log.action')
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="Downloadfile">下载文件</button>
                <button class="layui-btn layui-btn-sm" lay-event="CleanFile">清空文件</button>
                <button class="layui-btn layui-btn-sm" lay-event="DeleteFile">删除文件</button>
                <button class="layui-btn layui-btn-sm" lay-event="DeleteFileAll">清空当前目录</button>
            </div>
            @endcan
        </script>

        <script type="text/javascript">
            layui.use(['form', 'table', 'element'], function () {
                console.log("{{$current_folder}}");
                var form = layui.form;
                var table = layui.table;
                var element = layui.element;

                //监听导航点击
                element.on('nav(log-nav)', function(elem) {
//                layer.msg(elem.data('folder'));
//                layer.msg(elem.data('file'));
                    var f = elem.data('folder');
                    var l = elem.data('file');
                    //console.log(l);
                    if (l == undefined) {
                        location.href = "{{ route('admin.log') }}?f=" + elem.data('folder');
                    } else {
                        if (f == undefined) {
                            f = '';
                        }
                        $("input[name='l']").val(l);
                        $("input[name='f']").val(f);

                        //location.href = "{{ route('admin.log') }}?l=" + l + "&f=" + f;
                        dataTable.reload({
                            where: {f: f, l: l},
                            page: {curr: 1},
                            url: "{{ route('admin.log') }}"
                        });
                    }
                });

                var dataTable = table.render({
                    elem: '#LAY-log-data'
                    , height: 560
                    , url: "{{ route('admin.log') }}?l={{ \Illuminate\Support\Facades\Crypt::encrypt($current_file) }}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        , {field: 'level', title: "等级", templet: '#level', width: 60}
                        , {field: 'context', title: 'Context', width: 60}
                        , {field: 'date', title: '日期', width: 165}
                        , {field: 'text', title: '内容', templet: '#context', width: 650}
                    ]],
                    @can('system.log.action')
                    toolbar: "#buttons"
                    @endcan
                });

                form.on('select(file)', function (data) {
                    var type = $(data.elem).data('type');
                    active[type] ? active[type].call(this, data.value) : '';
                });

                var active = {
                    changeLogfile: function (file) {
                        dataTable.reload({
                            where: {l: file},
                            page: {curr: 1}
                        });
                    },
                    @can('system.log.action')
                    ajaxs: function (current_file, type) {
                        var f = $("input[name='f']").val();
                        if (type == 'dl') {
                            location.href = "{{ route('admin.log.action') }}?" + type + "=" + current_file + "&f=" + f;
                        } else {
                            layer.confirm('确定操作吗?', {
                                offset: '100px'
                            }, function () {
                                var ajax_lod = layer.load(1);
                                $.ajax({
                                    type: "POST",
                                    url: '{{ route('admin.log.action') }}?' + type + "=" + current_file + "&f=" + f,
                                    data: {
                                        _token: '{{ csrf_token() }}'
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        layer.close(ajax_lod);
                                        if (data.status == 1) {
                                            layer.msg(data.msg, {icon: 1, offset: '100px'})
                                        }
                                        setTimeout(function () {
                                            location.reload()
                                        }, 500);
                                    }
                                    , error: function (XMLHttpRequest, textStatus, errorThrow) {
                                        layer.close(ajax_lod);
                                        var ret_json = XMLHttpRequest.responseJSON;
                                        layer.msg('请求失败,系统异常:' + ret_json.message, {
                                            icon: 5,
                                            anim: 6,
                                            offset: '100px'
                                        })
                                    }
                                });
                            })
                        }
                    }
                    @endcan
                };

                //工具头监听
                @can('system.log.action')
                table.on('toolbar(LAY-log-data)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var l = $("input[name='l']").val();
                    switch (obj.event) {
                        case 'Downloadfile':
                            active.ajaxs.call(this, l, 'dl');
                            break;
                        case 'CleanFile':
                            active.ajaxs.call(this, l, 'clean');
                            break;
                        case 'DeleteFile':
                            active.ajaxs.call(this, l, 'del');
                            break;
                        case 'DeleteFileAll':
                            active.ajaxs.call(this, l, 'delall');
                            break;
                    }
                });
                @endcan
            });
        </script>
    @endcan
@endsection